/**
 * 
 */
package com.alstudio.view.popwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.ui.base.BaseActivity;
import com.alstudio.utils.android.media.audio.recorder.ALAudioRecordTaskInfo;
import com.alstudio.utils.log.ALLog;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class ALPopWindowUtils {

	/**
	 * 传入一个view，创建popwindow对象
	 * 
	 * @param mHandler
	 * @param ctx
	 * @return
	 */
	public static PopupWindow createPopupWindowWithView(Context ctx, View view) {
		final PopupWindow pop = new PopupWindow(view, LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		pop.setFocusable(true);
		pop.setOutsideTouchable(true);
		pop.setBackgroundDrawable(new BitmapDrawable());
		return pop;
	}

	public static void showPopWindow(PopupWindow window, View parent) {
		// window.showAtLocation/*showAsDropDown(parent, 0, 0,
		// Gravity.CENTER_HORIZONTAL)*//*(parent)*/(parent,
		// Gravity.CENTER_HORIZONTAL, 0, 0);
		window.showAsDropDown(parent, 0, 30);
	}

	/**
	 * 显示按住说话对话框
	 * 
	 * @param ctx
	 * @return
	 */
	public static PopupWindow showRecordWindow(Context ctx) {

		// 加载view
		View view = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater()
				.inflate(R.layout.new_record_window, null);

		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		pop.setFocusable(true);
		pop.setOutsideTouchable(true);
		pop.setBackgroundDrawable(new BitmapDrawable());
		pop.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
			}
		});

		ALAudioRecordTaskInfo info;

		// 关闭对话框
		view.findViewById(R.id.icon_close).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						pop.dismiss();
					}
				});
		// 预览语音签名
		view.findViewById(R.id.play).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ALLog.d( "开始播放");
			}
		});

		// 重录
		view.findViewById(R.id.press_to_recall).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ALLog.d( "重新录制");
					}
				});

		// 保存
		view.findViewById(R.id.press_to_save).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ALLog.d( "开始保存");
					}
				});

		// 按住说话
		view.findViewById(R.id.press_to_talk).setOnTouchListener(
				new OnTouchListener() {

					@Override
					public boolean onTouch(View arg0, MotionEvent arg1) {
						// TODO Auto-generated method stub
						return false;
					}
				});

		return pop;

	}
	
	public static ALAudioRecordTaskInfo showRecordWindow(final BaseActivity ctx,
			boolean touchAble) {

		// 加载view
		View view = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater()
				.inflate(R.layout.new_record_window, null);

		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		if (touchAble) {
			pop.setFocusable(true);
			pop.setOutsideTouchable(true);
			pop.setBackgroundDrawable(new BitmapDrawable());
		} else {
			pop.setFocusable(true);
		}
		
		pop.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				ctx.exitBlur();
			}
		});

		ALAudioRecordTaskInfo info = new ALAudioRecordTaskInfo();

		info.setRecordingBeatView((ProgressBar) view
				.findViewById(R.id.recording_beat));

		info.setRecordTimeView((TextView) view.findViewById(R.id.record_time));

		// 关闭对话框
		info.setCloseIconView(view.findViewById(R.id.icon_close));

		info.setSaveButtonLayout(view.findViewById(R.id.rec_layout));

		// 预览语音签名
		info.setPlayView(view.findViewById(R.id.play));

		// 重录
		info.setPressToRecallView(view.findViewById(R.id.press_to_recall));

		// 保存
		info.setPressToSaveView(view.findViewById(R.id.press_to_save));

		// 按住说话
		info.setPressToTalkView(view.findViewById(R.id.press_to_talk));

		// 左边按钮
		info.setNegative_button((TextView) view
				.findViewById(R.id.negative_button));

		// 右边按钮
		info.setPostive_button((TextView) view
				.findViewById(R.id.postive_button));
		// 第二信息
		info.setPrice((TextView) view.findViewById(R.id.price));

		info.setWindow(pop);

//		pop.setAnimationStyle(R.style.PopupAnimation);
		pop.update();
		ctx.enterBlur();

		return info;

	}

}
